<template>
  <div class="t-form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :label-width="labelwidth">
      <el-row>
        <template v-for="item in formItems" :key="item.babel">
          <el-col v-bind="colLayout">
            <el-form-item :label="item.label" class="form-item" :style="itemStyle" v-if="!item.isShow">
              <template v-if="item.type === 'input' || item.type === 'password'">
                <el-input :placeholder="item.placeholder" :show-password="item.type === 'password'" size="medium"
                  v-bind="item.otherOptions" v-model="formData[`${item.field}`]" />
              </template>
              <template v-else-if="item.type === 'select'">
                <el-select :placeholder="item.placeholder" v-bind="item.otherOptions"
                  v-model="formData[`${item.field}`]" style="width: 100%">
                  <el-option v-for="option in item.options" :value="option.value" :key="option.value">
                    {{ option.title }}
                  </el-option>
                </el-select>
              </template>
              <template v-else-if="item.type === 'datepicker'">
                <el-date-picker v-bind="item.otherOptions" v-model="formData[`${item.field}`]">
                </el-date-picker>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
import type { PropType } from 'vue'
import type { IFormItem } from '../types'

const props = defineProps({
  modelValue: {
    type: Object,
    required: true
  },
  formItems: {
    type: Array as PropType<IFormItem[]>,
    default: () => []
  },
  labelwidth: {
    type: String,
    default: '250px'
  },
  colLayout: {
    type: Object,
    default: () => ({
      lg: 8,
      md: 12,
      sm: 24
    })
  },
  itemStyle: {
    type: Object,
    default: () => ({})
  }
})
const emit = defineEmits(['update:modelValue'])
const formData = ref({ ...props.modelValue })
watch(formData, (newValue) => emit('update:modelValue', newValue), {
  deep: true
})
</script>
<style lang="less">
.el-form {
  display: flex;
  padding-top: -20px;
  text-align: center;
  .el-row {
    margin-left: -80px;
    margin-right: 100px;
  }
  .el-col {
    border-radius: 4px;
  }
}
</style>
